package com.xing.hosp.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xing.common.result.Result;
import com.xing.hosp.service.HospitalService;
import com.xing.hospital.model.hosp.Hospital;
import com.xing.hospital.model.vo.hosp.HospitalQueryVo;
import com.xing.hospital.model.vo.msm.MsmVo;
import com.xing.rabbitmq.constant.MqConst;
import com.xing.rabbitmq.service.RabbitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "医院管理接口")
@RestController
@RequestMapping("/admin/hosp/hospital")
//@CrossOrigin
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private ObjectMapper objectMapper;


    //测试rabbitMQ发送消息
    @GetMapping("sendRabbitMQ")
    public Result sendRabbitMQ() {
        MsmVo msmVo = new MsmVo();
        msmVo.setPhone("15237295636");
        msmVo.setTemplateCode("发送模板");
        Map<String, Object> map = new HashMap();


        for (int i = 0; i < 1000; i++) {

            map.put("1", "map" + i);
            msmVo.setParam(map);
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, msmVo);
        }

        return Result.ok("生产者发送消息成功");

    }

    //医院列表
    @ApiOperation("获取分页列表,条件查询带分页")
    @GetMapping("list/{page}/{limit}")
    public Result listHosp(@PathVariable Integer page, @PathVariable Integer limit, HospitalQueryVo hospitalQueryVo) {
        Page<Hospital> pageModel = hospitalService.selectPage(page, limit, hospitalQueryVo);
        List<Hospital> content = pageModel.getContent();

        long totalElements = pageModel.getTotalElements();
        return Result.ok(pageModel);

    }

    //更新医院的上线状态
    @GetMapping("/updateHospStatus/{id}/{status}")
    public Result updateHospStatus(@PathVariable String id, @PathVariable int status) {
        hospitalService.updateHospStatus(id, status);
        return Result.ok();
    }

    /**
     * 医院详情信息
     *
     * @param id
     * @return
     */
    @GetMapping("showHospDetail/{id}")
    public Result showHospDetail(@PathVariable String id) {
        Map<String, Object> map = hospitalService.getHospById(id);
        return Result.ok(map);
    }
}
